<?php
define('is_running',true);
ini_set( 'default_charset', 'utf-8' );
error_reporting(E_ALL);

require('common.php');


global $rootDir,$config,$project;
$config = array();
$project = new phpproject();
$project->Run();

require('template.php');


class phpproject{

	var $content;
	var $startTime;
	var $title = 'Home';
	var $ready_and_writable = false;

	function phpproject(){

		$this->startTime = microtime();
		$this->Init();

		//message('<a href="http://code.google.com/p/closure-compiler/issues/detail?id=205" target="_blank">UTF Issue</a>');
		//http://groups.google.com/group/closure-compiler-discuss/browse_thread/thread/1345e67b388f8251/11d682335fdeebd3');

	}

	function GetTime(){
		echo '<p>';
		$duration = microtime_diff($this->startTime, microtime());
		echo $duration.' seconds';
		echo '</p>';
	}

	function Run(){
		ob_start();

		if( !$this->ready_and_writable ){
			$this->NotReady();
		}else{
			$this->RunInner();
		}

		$this->content = ob_get_clean();
	}

	function RunInner(){

		/* actions */
		$cmd = common::GetCommand();
		$show = true;
		switch($cmd){

			case 'checkphp';
				$this->title = 'Analyze PHP';
				require('php_version/php_version.php');
				new php_version_check();
				$show = false;
			break;

			case 'about':
				$this->About();
				$show = false;
			break;

			case 'save_config':
				$this->PostConfig();
			case 'config':
				$this->ShowConfig();
				$show = false;
			break;


			case 'min_project':
			case 'min_proj_prompt':
				$this->title = 'Minimize Package';
				require('package.php');
				new package($cmd);
				$show = false;
			break;

			case 'rmpackage':
				if( $this->RmPackage() ){
					$show = false;
				}
			break;
			case 'confirm_remove':
				$this->RmPackageConfirm();
			break;


			case 'Save Package':
				if( $this->SavePackage() ){
					break;
				}

			case 'newproject':
				$this->StartPackage();
				$show = false;
			break;

			case 'editpackage':
				$this->EditPackage();
				$show = false;
			break;
			case 'save_changes':
				if( !$this->SaveChanges() ){
					$show = false;
				}
			break;


			/*
			 * One file at a time
			 *
			 */
			case 'minfile':
				$this->MinFile();
				$show = false;
			break;

			case 'minifyjsfile';
			case 'Minimize Javascript':
				$this->MinimizeJS();
				$this->MinFile();
				$show = false;
			break;

			case 'Minimize CSS':
				$this->MinimizeCSS();
				$this->MinFile();
				$show = false;
			break;

		}
		if( $show ){
			$this->ShowPackages();
		}
	}

	function About(){
		$this->title = 'About PHP Minimizer';

		echo '<p>';
		echo 'PHP Minimizer can be used to organize your projects and compress CSS and JavaScript files. ';
		echo '</p>';

		echo '<h2>CssMin & Closure Compiler</h2>';
		echo '<p>';
		echo 'Minimizing CSS and Javascript is accomplished using <a href="http://code.google.com/p/cssmin/">CssMin</a> and <a href="http://code.google.com/closure/compiler/">Google\'s Closure Compiler</a>. ';
		echo '</p>';
		echo '<p>';
		echo 'phpEasyMin uses the Closure Compiler service API to make remote calls to the compiler. phpEasyMin will cache the responses to reduce overhead and help prevent reaching Closure Compiler\'s usage limit.';
		echo '</p>';


		echo '<h2>Example</h2>';
		echo '<p>The following example shows how phpEasyMin can reduce the number of files and reduce file sizes to quickly and easily create a downloadable package.';


		echo '<table class="bordered" cellpadding="0" cellspacing="0">';
		echo '<tr><th>Original Folder</th><th>Minimized Folder</th></tr>';
		echo '<tr><td>';
		echo '<ul><li>Number of Files: 781</li>';
		echo '<li>Size: 16.4 MB</li>';
		echo '<li>Javascript: 239 KB</li>';
		echo '<li>CSS: 6 KB</li>';
		echo '</ul>';
		echo '<p><img src="include/static/example1.png" /></p>';
		echo '</td><td>';
		echo '<ul><li>Number of Files: 20</li>';
		echo '<li>Size: 1.6 MB</li>';
		echo '<li>Javascript: 92 KB</li>';
		echo '<li>CSS: 5 KB</li>';
		echo '</ul>';
		echo '<p><img src="include/static/example2.png" /></p>';
		echo '</td></tr>';
		echo '</table>';



		echo '<p>';
		echo '<a href="http://phpEasyMin.com">phpEasyMin.com</a>';
		echo '</p>';

	}




	function Init(){
		global $rootDir, $config;
		$rootDir = str_replace('\\','/',dirname(dirname(__FILE__)));
		$configFile = $rootDir.'/data/x_config.php';

		if( !is_writable($rootDir.'/data') ){
			return false;
		}

		if( file_exists($configFile) && !is_writable($configFile) ){
			return false;
		}

		if( file_exists($configFile) ){
			require($configFile);
		}

		$config += array(
					'destination'=>$rootDir.'/output',
					'ignore_types'=>'svn psd xcf',
					'ignore_prefix'=>'x_ X_ _',
					);

		$this->ready_and_writable = true;
	}

	function NotReady(){
		global $rootDir;
		$this->title = 'Installation';

		echo '<p>';

		echo 'To get started, you must make the configuration file writable.';
		echo ' The location of your configuration file is <em>'.$rootDir.'/data/x_config.php</em>';
		echo '</p>';
		echo '<p>';
		echo 'If the configuration file does not exist yet, create it and make sure it is writable.';
		echo '</p>';


	}

	function SaveConfig(){
		global $rootDir,$config;
		$configFile = $rootDir.'/data/x_config.php';

		if( !is_writable($rootDir.'/data') ){
			message('<b>Warning:</b> The data directory is not writable: '.$rootDir.'/data');
			return false;
		}
		if( !is_array($config) ){
			return false;
		}

		return common::SaveArray($configFile,'config',$config);
	}



	/**
	 * Show the configuration form
	 * @To do
	 * 	- add ignore file types
	 *
	 *
	 *
	 */
	function ShowConfig(){
		global $config,$rootDir;
		$this->title = 'Configuration';

		echo '<form method="post" action="">';
		echo '<table class="bordered" cellspacing="0" cellpadding="0">';

		echo '<tr><th colspan="2">Configuration</th>';
		echo '</tr>';


		echo '<tr>';
		echo '<td>Default Destination</td>';
		echo '<td>';
		echo '<input type="text" class="text" name="destination" value="'.htmlspecialchars($config['destination']).'"/>';
		echo '</td>';
		echo '</tr>';

		echo '<tr>';
		echo '<td>Ignore Filetypes</td>';
		echo '<td>';
		echo '<input type="text" class="text" name="ignore_types" value="'.htmlspecialchars($config['ignore_types']).'"/>';
		echo '<div class="example">List of file types that should not be included in your packages. Example: "svn psd xcf"</div>';
		echo '</td>';
		echo '</tr>';

		echo '<tr>';
		echo '<td>Ignore Prefixes</td>';
		echo '<td>';
		echo '<input type="text" class="text" name="ignore_prefix" value="'.htmlspecialchars($config['ignore_prefix']).'"/>';
		echo '<div class="example">Ignore files that begin with these prefixes. Example: "x_ X_ _"</div>';
		echo '</td>';
		echo '</tr>';


		echo '<tr>';
		echo '<td>&nbsp;</td>';
		echo '<td>';
		echo ' <input type="hidden" name="cmd" value="save_config" />';
		echo ' <input type="submit" name="" value="Save Config" />';
		echo ' <input type="submit" name="cmd" value="Cancel" />';
		echo '</td>';
		echo '</tr>';



		echo '</table>';
		echo '</form>';
	}

	function PostConfig(){
		global $config;
		$save = true;

		//destination
		$dest = $_POST['destination'];
		if( !file_exists($dest) ){
			message('Sorry, the destination path you entered does not exist.');
			$save = false;
		}elseif( !is_writable($dest) ){
			message('Sorry, the destination path you entered is not writable.');
			$save = false;
		}else{
			$config['destination'] = $dest;
		}

		$config['ignore_types'] = $_POST['ignore_types'];
		$config['ignore_prefix'] = $_POST['ignore_prefix'];
		unset($config['ignore_beginning']);



		if( !$save ){
			return;
		}

		if( $this->SaveConfig() ){
			message('Your configuration was saved.');
		}else{
			message('Oops, the configuration was not saved. Please try again.');
		}

	}



	function Content(){
		echo $this->content;

	}
	function ShowPackages(){
		global $config;

		$empty = true;

		if( isset($config['packages']) ){
			echo '<table class="bordered" cellspacing="0" cellpadding="0">';


			$temp = array();
			foreach($config['packages'] as $key => $package ){
				$package['key'] = $key;
				$temp[$package['group']][] = $package;
			}

			foreach($temp as $group => $package_group ){
				echo '<tr><th colspan="2">';
				echo $group;
				echo '</th>';
				echo '</tr>';

				$i = 0;
				foreach($package_group as $id => $package ){
					echo '<tr class="'.( $i%2==0 ? 'even' : 'odd' ).'">';
					echo '<td>';
					echo '<a href="?cmd=min_proj_prompt&id='.$package['key'].'">'.$package['name'].'</a>';
					echo '</td>';
					echo '<td class="options">';
					echo '<a href="?cmd=min_proj_prompt&id='.$package['key'].'">Minimize...</a>';
					echo '<a href="?cmd=checkphp&id='.$package['key'].'">Check PHP</a>';
					echo ' <a href="?cmd=editpackage&id='.$package['key'].'">Edit</a>';
					echo ' <a href="?cmd=rmpackage&id='.$package['key'].'">Delete</a>';
					echo '</td>';
					echo '</tr>';
					$i++;
				}
				echo '<td>&nbsp;</td>';
			}
			echo '</table>';
		}else{
			echo '<p>';
			echo ' <a href="?cmd=newproject">Start a new project</a>';
			echo '</p>';
		}
	}



	function RmPackageConfirm(){
		global $config;

		$id = $_GET['id'];
		if( !isset($config['packages'][$id]) ){
			return false;
		}
		unset($config['packages'][$id]);
		$this->SaveConfig();
	}

	function RmPackage(){
		global $config;

		$id = $_GET['id'];
		if( !isset($config['packages'][$id]) ){
			return false;
		}
		$package =& $config['packages'][$id];

		echo '<form method="post" action="">';
		echo '<p>';
		echo 'Are you sure you want to remove package <em>'.$package['name'].'</em>.';
		echo '<input type="hidden" name="id" value="'.$id.'"/>';
		echo '<input type="hidden" name="cmd" value="confirm_remove"/>';
		echo ' <input type="submit" name="" value="Confirm" />';
		echo ' <input type="submit" name="cmd" value="Cancel" />';
		echo '</p>';
		echo '</form>';

		return true;
	}




	/*
	 *
	 * Creating and Editing Package Information
	 *
	 *
	 */

	function CheckPackageInfo(){

		if( !file_exists($_POST['source']) ){
			message('Source does not exist.');
			return false;
		}
		if( !file_exists($_POST['destination']) ){
			message('Destination does not exist.');
			return false;
		}
		if( empty($_POST['name']) ){
			message('Name cannot be empty');
			return false;
		}

		return true;
	}

	function SavePackage(){
		global $config;

		if( !$this->CheckPackageInfo() ){
			return false;
		}

		$package = array();
		$package['name'] = htmlspecialchars($_POST['name']);
		$package['group'] = substr(htmlspecialchars($_POST['group']),0,30);
		$package['source'] = rtrim($_POST['source'],'/');
		$package['destination'] = rtrim($_POST['destination'],'/');
		$package['noshrink'] = $_POST['noshrink'];

		$config['packages'][] = $package;
		$this->SaveConfig();

		return true;

	}


	function SaveChanges(){
		global $config;


		$id = $_POST['id'];
		if( !isset($config['packages'][$id]) ){
			message('Invalid request.');
			return true;
		}

		if( !$this->CheckPackageInfo() ){
			$this->EditPackage($_POST);
			return false;
		}

		$package =& $config['packages'][$id];
		$package['name'] = htmlspecialchars($_POST['name']);
		$package['group'] = substr(htmlspecialchars($_POST['group']),0,30);
		$package['source'] = rtrim($_POST['source'],'/');
		$package['destination'] = rtrim($_POST['destination'],'/');
		$package['noshrink'] = $_POST['noshrink'];

		$this->SaveConfig();
		return true;
	}

	function EditPackage($package = false){
		global $rootDir,$config;

		$id = $_REQUEST['id'];
		if( !isset($config['packages'][$id]) ){
			message('Invalid request.');
			return false;
		}

		if( $package === false ){
			$package = $_REQUEST;
			$package =& $config['packages'][$id];
		}

		$this->title = $package['name'].' > Edit';

		echo '<form method="post" action="">';
		echo '<input type="hidden" name="id" value="'.$id.'" />';
		$this->DetailsForm($package);
		echo '<tr>';
			echo '<td>';
			echo '&nbsp;';
			echo '</td>';
			echo '<td>';
			echo ' <input type="submit" name="" value="Save Changes" />';
			echo ' <input type="hidden" name="cmd" value="save_changes" />';
			echo ' <input type="submit" name="cmd" value="Cancel" />';
			echo '</td>';
			echo '</tr>';
		echo '</table>';
		echo '</form>';


/*
		echo '<h2>Package</h2>';
		echo showArray($package);
		echo '<h2>Config</h2>';
		echo showArray($config);
*/

	}

	function DetailsForm($vars = array() ){
		global $rootDir,$config;

		$vars += array('source'=>'','destination'=>'','name'=>'','new_group'=>'','group'=>'','noshrink'=>'');

		echo '<table class="bordered" cellpadding="0" cellspacing="0">';

		echo '<tr>';
		echo '<th>Option </th>';
		echo '<th>';
		echo 'Value';
		echo '</th>';
		echo '</tr>';

		echo '<tr>';
		echo '<td>Name </td>';
		echo '<td>';
		echo '<input type="text" size="40" class="text" name="name" value="'.htmlspecialchars($vars['name']).'"/>';
		echo '</td>';
		echo '</tr>';

		echo '<tr>';
		echo '<td>Source </td>';
		echo '<td>';
		echo '<input type="text" size="40" class="text" name="source" value="'.htmlspecialchars($vars['source']).'"/>';
		echo '<div class="example">Example: '.$rootDir.'/include</div>';
		echo '</td>';
		echo '</tr>';

		echo '<tr>';
		echo '<td>Destination </td>';
		echo '<td>';
		if( empty($vars['destination']) ){
			$temp = $rootDir.'/output';
			if( is_writable($temp) ){
				$vars['destination'] = $temp;
			}
		}
		echo '<input type="text" size="40" class="text" name="destination" value="'.htmlspecialchars($vars['destination']).'"/>';
		echo '<div class="example">Example: '.$rootDir.'/output</div>';
		echo '</td>';
		echo '</tr>';

		echo '<tr>';
			echo '<td>';
			echo 'Group ';
			echo '</td>';
			echo '<td>';


			$groups = array();
			if( isset($config['packages']) ){
				foreach($config['packages'] as $key => $package ){
					$groups[$package['group']] = $package['group'];
				}
			}
			$groups['Default'] = 'Default';


			echo '<div class="group_input">';
			echo '<select name="group">';
			foreach($groups as $key => $value){
				if( $vars['group'] == $key ){
					echo '<option value="'.$key.'" selected="selected">'.$value.'</option>';
				}else{
					echo '<option value="'.$key.'">'.$value.'</option>';
				}
			}
			echo '</select>';
			echo '<br/>';
			echo '<a href="#" class="toggle_group">Add New Group</a>';
			echo '</div>';

			echo '<div class="group_input" style="display:none">';
			echo '<input type="text" size="40" class="text" name="group_hidden" value="'.htmlspecialchars($vars['new_group']).'" />';
			echo '<br/>';
			echo '<a href="#" class="toggle_group">Select From Existing</a>';
			echo '</div>';
			echo '</td>';
			echo '</tr>';


		echo '<tr>';
		echo '<td>NoShrink Paths</td>';
		echo '<td>';
		echo '<textarea name="noshrink" rows="3" wrap="off">';
		echo htmlspecialchars($vars['noshrink']);
		echo '</textarea>';
		echo '<br/>';
		echo 'Newline separated list of paths to copy but not minimize';
		echo '</td>';
		echo '</tr>';
	}

	function StartPackage(){
		global $rootDir,$config;
		$this->title = 'New Project';

		$_POST += array('destination'=>$config['destination']);

		echo '<form method="post" action="">';
		$this->DetailsForm($_POST);

		echo '<tr>';
		echo '<td>&nbsp;</td>';
		echo '<td>';
		echo ' <input type="submit" name="cmd" value="Save Package" />';
		echo ' <input type="submit" name="cmd" value="Cancel" />';
		echo '</td>';
		echo '</tr>';

		echo '</table>';
		echo '</form>';


	}













	/*
	 *
	 * Working With individual files
	 *
	 */

	function MinFile(){
		$this->title = 'Minimize File';


		$_REQUEST += array('code'=>'');

		echo '<form action="" method="post"  enctype="multipart/form-data">';
		echo '<table class="bordered" cellpadding="0" cellspacing="0">';
		echo '<tr>';
			echo '<th colspan="2">';
			echo 'Minimize File';
			echo '</th>';
			echo '</tr>';
		echo '<tr>';
			echo '<td class="nowrap">';
			echo 'Select a File';
			echo '</td>';
			echo '<td>';
			echo '<input type="file" class="text" size="15" name="file" />';
			echo '</td>';
			echo '</tr>';
		echo '<tr>';
			echo '<td class="nowrap">';
			echo 'or';
			echo '</td>';
			echo '<td>';
			echo '</td>';
			echo '</tr>';

		echo '<tr>';
			echo '<td class="nowrap">';
			echo 'Paste the Text';
			echo '</td>';
			echo '<td>';
			echo '<textarea cols="50" rows="7" name="code">'.htmlspecialchars($_REQUEST['code']).'</textarea>';
			echo '</td>';
			echo '</tr>';

		echo '<tr>';
			echo '<td>';
			echo '&nbsp;';
			echo '</td>';
			echo '<td>';
			echo ' <input type="submit" name="cmd" value="Minimize Javascript" />';
			echo ' <input type="submit" name="cmd" value="Minimize CSS" />';
			echo '</td>';
			echo '</tr>';
		echo '</table>';
		echo '</form>';
	}



	/**
	 * Minimize a single css file
	 *
	 */
	function MinimizeCSS(){
		$content = $this->GetPostedContent();
		if( !$content ) return;

		$result = phpproject::MinCss($content);
		$this->MinimizedResults($content,$result);
	}


	/**
	 * Minimize a single javascript file
	 *
	 */
	function MinimizeJS(){

		$content = $this->GetPostedContent();
		if( !$content ) return;

		$result = phpproject::FetchFromClosure($content);

		if( $result === false ){
			message('Sorry, your code was not successfully minified. Check to make sure you\'re connected to the internet.');
			return;
		}

		$result = json_decode($result,true);

		$this->MinimizedResults($content,$result['compiledCode']);
		//echo showArray($result);
	}

	function MinimizedResults($orig,$result){

		$orig_size = strlen($orig);
		$min_size = strlen($result);


		echo '<table class="bordered" cellpadding="0" cellspacing="0">';
		echo '<tr>';
			echo '<th colspan="2">';
			echo 'Minimized Result';
			echo '</th>';
			echo '</tr>';

		echo '<tr>';
			echo '<td class="nowrap">';
			echo 'Original Size';
			echo '</td>';
			echo '<td>';
			echo number_format($orig_size).' bytes';
			echo '</td>';
			echo '</tr>';

		echo '<tr>';
			echo '<td class="nowrap">';
			echo 'Minimized Size';
			echo '</td>';
			echo '<td>';
			echo number_format($min_size).' bytes';
			echo '</td>';
			echo '</tr>';

		echo '<tr>';
			echo '<td class="nowrap">';
			echo 'Reduction';
			echo '</td>';
			echo '<td>';
			$percent = 0;
			if( $orig_size > 0 ){
				$percent = ($orig_size - $min_size) / $orig_size;
			}
			echo (number_format($percent,2)*100).'%';
			echo '</td>';
			echo '</tr>';

		echo '<tr>';
			echo '<td class="nowrap">';
			echo 'Minimized Text';
			echo '</td>';
			echo '<td>';
			echo '<textarea cols="70" rows="20" readonly="readonly" onclick="this.select()" class="result">'.htmlspecialchars($result).'</textarea>';
			echo '</td>';
			echo '</tr>';

		echo '</table>';


	}

	function GetPostedContent(){

		if( !empty($_FILES['file']['name']) ){

			if( !empty($_FILES['file']['error']) ){
				message('Oops, there was an error uploading your file. Please try again. (Upload Error 1)');
				return false;
			}

			$location = $_FILES['file']['tmp_name'];
			$contents = file_get_contents($location);
			if( !$contents ){
				message('Oops, there was an error uploading your file. Please try again. (Upload Error 2)');
				return false;
			}

			return $contents;
		}

		if( !empty($_POST['code']) ){
			return $_POST['code'];
		}

		return false;
	}











	/**
	 * Functions for using cssmin
	 */
	function MinCss($content){
		if( defined('cssmin_v3') ){
			return CssMin::minify($content);
		}

		return cssmin::minify($content, 'remove-last-semicolon,preserve-urls');
	}





	/**
	 *  Functions for fetching/storing minimized javascript from google closure compiler
	 *
	 */
	function MinimizeJSFile($path,$root=false){
		$result = $this->GetClosureResponse($path);

		$showPath = $path;
		if( $root !== false ){
			$len = strlen($root);
			$showPath = '...'.substr($path,$len);
		}


		if( !$result ){
			return false;
		}
		if( isset($result['errors']) ){
			message('Errors found in JavaScript file: '.$showPath.' '.showArray($result['errors']));
			return false;
		}
		if( isset($result['serverErrors']) ){
			message('Errors occurred while compressing JavaScript file: '.$showPath.' '.showArray($result['serverErrors']));
			return false;
		}

		//don't think this will ever happen
		if( !isset($result['compiledCode']) ){
			message('No Compiled Code for file: '.$showPath);
			return false;
		}

		if( isset($result['warnings']) ){
			message('Warnings found for JavaScript file: <a href="#" class="jswarnings">'.$showPath.'</a>.<div class="jswarnings">'.showArray($result['warnings']).'</div>');
		}

		return $result;
	}

	/**
	 * Get the full closure compiler response for $path
	 * Handles checking the cache and getting directly from closure compilers
	 * @return mixed
	 *
	 */
	function GetClosureResponse($path){

		$result = $this->GetCachedJS($path);
		if( $result !== false ){
			return json_decode($result,true);
		}

		$contents = file_get_contents($path);
		$result = phpproject::FetchFromClosure($contents);

		if( !$result ){
			message('Fetching minimized code from google failed.');
			return false;
		}
		$this->SaveToCache($path,$result);

		$array = json_decode($result,true);

		return $array;
	}


	/**
	 * Get the cached minimized js if the cache is newer than file being minimized
	 * @return mixed
	 *
	 */
	function GetCachedJS($path){
		global $rootDir;

		$cache_file = $this->CachePath($path);
		if( !file_exists($cache_file) ){
			return false;
		}

		$source_modified = filemtime($path);
		$cached_modified = filemtime($cache_file);
		if( $source_modified > $cached_modified ){
			return false;
		}

		return file_get_contents($cache_file);
	}

	function SaveToCache($path,$code){

		$cache_file = $this->CachePath($path);

		return common::Save($cache_file,$code);
	}

	function CachePath($path){
		global $rootDir;

		//reuse old cache files
		$cache_file = $rootDir.'/data/_cache/'.base64_encode($path);
		if( file_exists($cache_file) ){
			return $cache_file;
		}


		// "/" = 47
		// a = "97"
		$path = str_replace('\\','/',$path);
		$count = count_chars($path);

		return $rootDir.'/data/_cache/'.$count[47].'/'.$count[97].'/'.base64_encode($path);
	}



	//use google's closure compiler
	//http://closure-compiler.appspot.com/home
	function FetchFromClosure($content){


		$host = 'closure-compiler.appspot.com';
		$path = '/compile';
		$port = '80';

		//data
		$req = 'js_code='.urlencode($content);
		$req .= '&output_format=json';
		$req .= '&output_info=compiled_code&output_info=warnings&output_info=errors&output_info=statistics';
		$req .= '&compilation_level=SIMPLE_OPTIMIZATIONS';


		//request
		$http_request  = "POST $path HTTP/1.0\r\n";
		$http_request .= "Host: $host\r\n";
		$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
		$http_request .= "Content-Length: " . strlen($req) . "\r\n";
		$http_request .= "User-Agent: phpEasyMin\r\n";
		$http_request .= "\r\n";
		$http_request .= $req;


		if( false == ( $fs = fsockopen($host, $port, $errno, $errstr, 10) ) ) {
			message('Could not open socket');
			return false;
		}

		fwrite($fs, $http_request);

		$response = '';
		while ( !feof($fs) ){
			$response .= fgets($fs, 1160); // One TCP-IP packet
		}
		fclose($fs);

		$response = explode("\r\n\r\n", $response, 2);
		if( empty($response[1]) ){
			return false;
		}

		return $response[1];
	}



	function QuickForm(){
		$_POST += array('source'=>'','destination'=>'','file'=>'');


		echo '<div id="links_main">';
		echo '<h4>Minimize</h4>';
		echo '<ul>';
		echo '<li><a href="?">EasyMin Home</a></li>';
		echo '<li><a href="?cmd=newproject">Add New Project</a></li>';
		echo '<li><a href="?cmd=minfile">Minimize File</a></li>';
		echo '</ul>';

		echo '<h4>phpEasyMin Info</h4>';
		echo '<ul>';
		echo '<li><a href="?cmd=config">Configuration</a></li>';
		echo '<li><a href="?cmd=about">About PHP Minimizer</a></li>';
		echo '</ul>';

		echo '<h4>Min Resources</h4>';
		echo '<ul>';
		echo '<li><a href="http://code.google.com/p/cssmin/">CssMin</a></li>';
		echo '<li><a href="http://code.google.com/closure/compiler/docs/gettingstarted_api.html">Closure Compiler</a></li>';
		echo '</ul>';

		echo '<h4>Links</h4>';
		echo '<ul>';
		echo '<li><a href="http://gpeasy.com" title="Easy to use Content Managment System (CMS)">gpEasy CMS</a></li>';
		echo '</ul>';

		echo '</div>';
	}
}
